#include "StartScene.h"

USING_NS_CC;
using namespace ui;

Scene* StartScene::createScene()
{
    // 'scene' is an autorelease object
	auto scene = Scene::create();

	// 'layer' is an autorelease object
	auto layer = StartScene::create();

	// add layer as a child to scene
	scene->addChild(layer);

	// return the scene
	return scene;
}

bool StartScene::init()
{
	Size s = Director::getInstance()->getVisibleSize();
	//开始界面背景
	yuan = Sprite::create("res/yuan.png");
	yuan->setScale(s.width / 900);
	yuan->setPosition(Point(s.width / 2, 0));
	this->addChild(yuan,1);
	mid = Sprite::create("res/mground.png");
	mid->setScale(s.width / 900);
	mid->setPosition(Point(s.width / 2, -s.height * 0.5));
	this->addChild(mid,2);
	jin = Sprite::create("res/jin.png");
	jin->setScale(s.width / 900);
	jin->setPosition(Point(s.width / 2, -s.height));
	this->addChild(jin,3);
	vg = 0.25*s.width / 900;
	
	
	titleAction();
	
	//start按钮
	star = Button::create();
	star->setTouchEnabled(true);
	star->loadTextures("res/Start.png", "res/Start.png", "");
	star->setScale(s.width / 900);
	star->setPosition(Point(s.width / 2, s.height * 2 / 5));
	star->addTouchEventListener(this, toucheventselector(StartScene::ButtentouchEvent));
	this->addChild(star,4);

	contin = Button::create();
	contin->setTouchEnabled(true);
	contin->loadTextures("res/continue.png", "res/continue.png", "");
	contin->setScale(s.width / 900);
	contin->setPosition(Point(s.width / 2, s.height / 2));
	contin->addTouchEventListener(this, toucheventselector(StartScene::contintouchEvent));
	//this->addChild(contin);
	//contin->setVisible(false);
	
	newGame = Button::create();
	newGame->setTouchEnabled(true);
	newGame->loadTextures("res/new game.png", "res/new game.png", "");
	newGame->setScale(s.width / 900);
	newGame->setPosition(Point(s.width / 2, s.height * 11 / 24));
	newGame->addTouchEventListener(this, toucheventselector(StartScene::newGametouchEvent));
	this->addChild(newGame,4);
	newGame->setVisible(false);

	option = Button::create();
	option->setTouchEnabled(true);
	option->loadTextures("res/option.png", "res/option.png", "");
	option->setScale(s.width / 900);
	option->setPosition(Point(s.width / 2, s.height * 9 / 24));
	option->addTouchEventListener(this, toucheventselector(StartScene::optiontouchEvent));
	this->addChild(option,4);
	option->setVisible(false);


	quit = Button::create();
	quit->setTouchEnabled(true);
	quit->loadTextures("res/quit.png", "res/quit.png", "");
	quit->setScale(s.width / 900);
	quit->setPosition(Point(s.width / 2, s.height * 7 / 24));
	quit->addTouchEventListener(this, toucheventselector(StartScene::quittouchEvent));
	this->addChild(quit,4);
	quit->setVisible(false);

	
	//音乐
	//加载音乐
	CocosDenshion::SimpleAudioEngine::sharedEngine()->preloadBackgroundMusic("res/startM.mp3");
	//设置默认音量
	CocosDenshion::SimpleAudioEngine::sharedEngine()->setBackgroundMusicVolume(50);
	//播放背景音乐
	CocosDenshion::SimpleAudioEngine::sharedEngine()->playBackgroundMusic("res/startM.mp3", true);
	//开始游戏循环
	scheduleUpdate();

	return true;
}
void StartScene::ButtentouchEvent(cocos2d::Object *pSender, cocos2d::ui::TouchEventType type)
{
	Button* butten = (Button*)pSender;
	unsigned int tag = butten->getTag();
	switch (type)
	{
	case ui::TOUCH_EVENT_BEGAN:
		butten->setPositionY(butten->getPositionY() - 10);
		break;

	case ui::TOUCH_EVENT_MOVED:

		break;

	case ui::TOUCH_EVENT_ENDED:
		butten->setPositionY(butten->getPositionY() + 10);

		this->removeChild(star);
		newGame->setVisible(true);
		option->setVisible(true);
		quit->setVisible(true);
		this->removeChild(startScene);
		titleAction2();
		
		//contin->setVisible(true);
		break;

	case ui::TOUCH_EVENT_CANCELED:
		butten->setPositionY(butten->getPositionY() + 10);
		break;

	default:
		break;
	}
}
void StartScene::newGametouchEvent(cocos2d::Object *pSender, cocos2d::ui::TouchEventType type)
{
	Button* butten = (Button*)pSender;
	unsigned int tag = butten->getTag();
	switch (type)
	{
	case ui::TOUCH_EVENT_BEGAN:
		butten->setPositionY(butten->getPositionY() - 10);
		break;

	case ui::TOUCH_EVENT_MOVED:

		break;

	case ui::TOUCH_EVENT_ENDED:
		butten->setPositionY(butten->getPositionY() + 10);

		Director::getInstance()->replaceScene(TransitionFade::create(1.2f, HelloWorld::createScene()));

		break;

	case ui::TOUCH_EVENT_CANCELED:
		butten->setPositionY(butten->getPositionY() + 10);
		break;

	default:
		break;
	}
}
void StartScene::optiontouchEvent(cocos2d::Object *pSender, cocos2d::ui::TouchEventType type)
{
	Button* butten = (Button*)pSender;
	unsigned int tag = butten->getTag();
	switch (type)
	{
	case ui::TOUCH_EVENT_BEGAN:
		butten->setPositionY(butten->getPositionY() - 10);
		break;

	case ui::TOUCH_EVENT_MOVED:

		break;

	case ui::TOUCH_EVENT_ENDED:
		butten->setPositionY(butten->getPositionY() + 10);

		
		break;

	case ui::TOUCH_EVENT_CANCELED:
		butten->setPositionY(butten->getPositionY() + 10);
		break;

	default:
		break;
	}
}
void StartScene::contintouchEvent(cocos2d::Object *pSender, cocos2d::ui::TouchEventType type)
{
	Button* butten = (Button*)pSender;
	unsigned int tag = butten->getTag();
	switch (type)
	{
	case ui::TOUCH_EVENT_BEGAN:
		butten->setPositionY(butten->getPositionY() - 10);
		break;

	case ui::TOUCH_EVENT_MOVED:

		break;

	case ui::TOUCH_EVENT_ENDED:
		butten->setPositionY(butten->getPositionY() + 10);

		
		break;

	case ui::TOUCH_EVENT_CANCELED:
		butten->setPositionY(butten->getPositionY() + 10);
		break;

	default:
		break;
	}
}
void StartScene::quittouchEvent(cocos2d::Object *pSender, cocos2d::ui::TouchEventType type)
{
	Button* butten = (Button*)pSender;
	unsigned int tag = butten->getTag();
	switch (type)
	{
	case ui::TOUCH_EVENT_BEGAN:
		butten->setPositionY(butten->getPositionY() - 10);
		break;

	case ui::TOUCH_EVENT_MOVED:

		break;

	case ui::TOUCH_EVENT_ENDED:
		butten->setPositionY(butten->getPositionY() + 10);
		
		Director::getInstance()->end();
		break;

	case ui::TOUCH_EVENT_CANCELED:
		butten->setPositionY(butten->getPositionY() + 10);
		break;

	default:
		break;
	}
}

void StartScene::titleAction()
{
	Size s = Director::getInstance()->getVisibleSize();
	Animation *titleA = Animation::create();
	for (int i = 1; i < 41 ; i++)
	{
		char szImageFileName[256] = { 0 };
		sprintf(szImageFileName, "res/%d.png", i);
		titleA->addSpriteFrameWithFile(szImageFileName);
	}
	titleA->setDelayPerUnit(0.04f);
	titleA->setRestoreOriginalFrame(true);
	//titleA->setLoops(1000000);
	auto titleAc = Animate::create(titleA);
	startScene = Sprite::create();
	startScene->setScale(s.width / 900);
	startScene->setPosition(Point(s.width / 2, s.height * 3 / 4));
	this->addChild(startScene,4);

	startScene->runAction(RepeatForever::create(dynamic_cast<CCActionInterval *>(Sequence::create(titleAc,titleAc->reverse(),NULL))));
}
void StartScene::titleAction2()
{
	Size s = Director::getInstance()->getVisibleSize();
	Animation *titleA = Animation::create();
	for (int i = 0; i < 2; i++)
	{
		char szImageFileName[256] = { 0 };
		sprintf(szImageFileName, "res/title%d.png", i);
		titleA->addSpriteFrameWithFile(szImageFileName);
	}
	titleA->setDelayPerUnit(0.7f);
	titleA->setRestoreOriginalFrame(true);
	
	auto titleAc = Animate::create(titleA);
	startScene = Sprite::create();
	startScene->setScale(s.width / 900);
	startScene->setPosition(Point(s.width / 2, s.height * 3 / 4));
	this->addChild(startScene,4);

	startScene->runAction(RepeatForever::create(dynamic_cast<CCActionInterval *>(Sequence::create(titleAc, titleAc->reverse(), NULL))));
}
void StartScene::update(float dt)
{
	Size s = Director::getInstance()->getVisibleSize();
	ji = jin->getPositionY();
	ji += 3 * vg;
	jin->setPositionY(ji);
	mi = mid->getPositionY();
	mi += 2 * vg;
	mid->setPositionY(mi);
	yu = yuan->getPositionY();
	yu += vg;
	yuan->setPositionY(yu);
	if (jin->getPositionY() <= -s.height)
	{
		vg = -vg;
	}
	

	


	if (jin->getPositionY() >= s.height*2)
	{
		vg = -vg;
	}
	
	

	
}